module guessing(acceptguesses, phrase0, phrase1, entry, keydata, clock50, hex2, hex3, hex4, hex5, hex6, hex7, whoshome0, whoshome1, gameoutcome, diekey);

input[1:0] acceptguesses;
input[127:0] phrase0;
input[127:0] phrase1;
input[15:0] entry;
input[16:0] keydata;
input clock50;
output[6:0] hex2;
output[6:0] hex3;
output[6:0] hex4;
output[6:0] hex5;
output[6:0] hex6;
output[6:0] hex7;
output[15:0] whoshome0;
output[15:0] whoshome1;
reg[15:0] newwhoshome1;
reg[15:0] whoshome0;
reg[15:0] whoshome1;
output[1:0] gameoutcome;
reg[1:0] gameoutcome;

reg testcomarison;
reg enableguesses;

output diekey;
reg diekey;

reg[15:0] whoshome1combine;

reg[15:0] whoshome10;
reg[15:0] whoshome11;
reg[15:0] whoshome12;
reg[15:0] whoshome13;
reg[15:0] whoshome14;
reg[15:0] whoshome15;
reg[15:0] whoshome16;
reg[15:0] whoshome17;
reg[15:0] whoshome18;
reg[15:0] whoshome19;
reg[15:0] whoshome1A;
reg[15:0] whoshome1B;
reg[15:0] whoshome1C;
reg[15:0] whoshome1D;
reg[15:0] whoshome1E;
reg[15:0] whoshome1F;

reg[8:0] currentchar;
reg[3:0] s;
reg[3:0] ns;
reg[25:0] guesslist;
reg checkguess;

parameter before = 5'd6;
parameter guess5 = 5'd5;
parameter guess4 = 5'd4;
parameter guess3 = 5'd3;
parameter guess2 = 5'd2;
parameter guess1 = 5'd1;
parameter guess0 = 5'd0;

blankHexDisplay disp2(hex2);
hexDisplay disp3(s, hex3);
hexDisplay disp4({3'b000, testcomarison}, hex4);
blankHexDisplay disp5(hex5);
hexDisplay disp6(currentchar[3:0], hex6);
hexDisplay disp7(currentchar[7:4], hex7);

always@(*)
begin
	case(s)
		before:
		begin	
			diekey = 1'b0;
			if(whoshome1 == 16'b0)
			begin
				ns = guess5;
				enableguesses = 1'b0;
			end
			else
			begin
				ns = before;
				enableguesses = 1'b0;
			end
		end
		guess5:
		begin
			enableguesses = 1'b1;
			if(entry == pressedENTER)
			begin
				ns = guess4;
				diekey = 1'b1;
			end
			else
			begin
				ns = guess5;
				diekey = 1'b0;
			end
		end
		guess4:
		begin
			enableguesses = 1'b1;
			if(entry == pressedENTER)
			begin
				diekey = 1'b1;
				ns = guess3;
			end
			else
			begin
				ns = guess4;
				diekey = 1'b0;
			end
		end
		guess3:
		begin
			enableguesses = 1'b1;
			if(entry == pressedENTER)
			begin
				ns = guess2;
				diekey = 1'b1;
			end
			else
			begin
				ns = guess3;
				diekey = 1'b0;
			end
		end
		guess2:
		begin
			enableguesses = 1'b1;
			if(entry == pressedENTER)
			begin
				ns = guess1;
				diekey = 1'b1;
			end
			else
			begin
				ns = guess2;
				diekey = 1'b0;
			end
		end
		guess1:
		begin
			enableguesses = 1'b1;
			if(entry == pressedENTER)
			begin
				ns = guess0;
				diekey = 1'b1;
			end
			else
			begin
				ns = guess1;
				diekey = 1'b0;
			end
		end
		guess0:
		begin
			ns = guess0;
			diekey = 1'b0;
		end
	endcase
end

always@(*)
begin
	case(acceptguesses)
		2'b00: currentchar = 9'h0FF;
		2'b01: currentchar = 9'h1FF;
		2'b10:
		begin
			case(keydata[15:0])
				pressedA: currentchar = {1'b1, uA};
				pressedB: currentchar = {1'b1, uB};
				pressedC: currentchar = {1'b1, uC};
				pressedD: currentchar = {1'b1, uD};
				pressedE: currentchar = {1'b1, uE};
				pressedF: currentchar = {1'b1, uF};
				pressedG: currentchar = {1'b1, uG};
				pressedH: currentchar = {1'b1, uH};
				pressedI: currentchar = {1'b1, uI};
				pressedJ: currentchar = {1'b1, uJ};
				pressedK: currentchar = {1'b1, uK};
				pressedL: currentchar = {1'b1, uL};
				pressedM: currentchar = {1'b1, uM};
				pressedN: currentchar = {1'b1, uN};
				pressedO: currentchar = {1'b1, uO};
				pressedP: currentchar = {1'b1, uP};
				pressedQ: currentchar = {1'b1, uQ};
				pressedR: currentchar = {1'b1, uR};
				pressedS: currentchar = {1'b1, uS};
				pressedT: currentchar = {1'b1, uT};
				pressedU: currentchar = {1'b1, uU};
				pressedV: currentchar = {1'b1, uV};
				pressedW: currentchar = {1'b1, uW};
				pressedX: currentchar = {1'b1, uX};
				pressedY: currentchar = {1'b1, uY};
				pressedZ: currentchar = {1'b1, uZ};
				default: currentchar = 9'h0FF;
			endcase
		end
		2'b11: currentchar = 9'h0FF;
	endcase
end

always@(*)
begin
	
//	if(s == before)
//	begin
//		whoshome10 <= 16'b0;
//		whoshome11 <= 16'b0;
//		whoshome12 <= 16'b0;
//		whoshome13 <= 16'b0;
//		whoshome14 <= 16'b0;
//		whoshome15 <= 16'b0;
//		whoshome16 <= 16'b0;
//		whoshome17 <= 16'b0;
//		whoshome18 <= 16'b0;
//		whoshome19 <= 16'b0;
//		whoshome1A <= 16'b0;
//		whoshome1B <= 16'b0;
//		whoshome1C <= 16'b0;
//		whoshome1D <= 16'b0;
//		whoshome1E <= 16'b0;
//		whoshome1F <= 16'b0;
//	end
	
	if(enableguesses == 1'b1)
	begin
		
		if(currentchar[7:0] == phrase1[7:0])
				whoshome10 <= 16'b0000_0000_0000_0001;
		else 	whoshome10 <= 16'b0000_0000_0000_0000;
		
		if(currentchar[7:0] == phrase1[15:8])
				whoshome11 <= 16'b0000_0000_0000_0010;
		else 	whoshome11 <= 16'b0000_0000_0000_0000;
		
		if(currentchar[7:0] == phrase1[23:16])
				whoshome12 <= 16'b0000_0000_0000_0100;
		else 	whoshome12 <= 16'b0000_0000_0000_0000;
		
		if(currentchar[7:0] == phrase1[31:24])
				whoshome13 <= 16'b0000_0000_0000_1000;
		else 	whoshome13 <= 16'b0000_0000_0000_0000;
		
		if(currentchar[7:0] == phrase1[39:32])
				whoshome14 <= 16'b0000_0000_0001_0000;
		else 	whoshome14 <= 16'b0000_0000_0000_0000;
		
		if(currentchar[7:0] == phrase1[47:40])
				whoshome15 <= 16'b0000_0000_0010_0000;
		else 	whoshome15 <= 16'b0000_0000_0000_0000;
		
		if(currentchar[7:0] == phrase1[55:48])
				whoshome16 <= 16'b0000_0000_0100_0000;
		else 	whoshome16 <= 16'b0000_0000_0000_0000;

		if(currentchar[7:0] == phrase1[63:56])
				whoshome17 <= 16'b0000_0000_1000_0000;
		else 	whoshome17 <= 16'b0000_0000_0000_0000;
		
		if(currentchar[7:0] == phrase1[71:64])
				whoshome18 <= 16'b0000_0001_0000_0000;
		else 	whoshome18 <= 16'b0000_0000_0000_0000;
		
		if(currentchar[7:0] == phrase1[79:72])
				whoshome19 <= 16'b0000_0010_0000_0000;
		else 	whoshome19 <= 16'b0000_0000_0000_0000;
		
		if(currentchar[7:0] == phrase1[87:80])
				whoshome1A <= 16'b0000_0100_0000_0000;
		else 	whoshome1A <= 16'b0000_0000_0000_0000;
		
		if(currentchar[7:0] == phrase1[95:88])
				whoshome1B <= 16'b0000_1000_0000_0000; // weird
		else 	whoshome1B <= 16'b0000_0000_0000_0000;
		
		if(currentchar[7:0] == phrase1[103:96])
				whoshome1C <= 16'b0001_0000_0000_0000;
		else 	whoshome1C <= 16'b0000_0000_0000_0000;
		
		if(currentchar[7:0] == phrase1[111:104])
				whoshome1D <= 16'b0010_0000_0000_0000;
		else 	whoshome1D <= 16'b0000_0000_0000_0000;
		
		if(currentchar[7:0] == phrase1[119:112])
				whoshome1E <= 16'b0100_0000_0000_0000;
		else 	whoshome1E <= 16'b0000_0000_0000_0000;
		
		if(currentchar[7:0] == phrase1[127:120])
				whoshome1F <= 16'b1000_0000_0000_0000;
		else 	whoshome1F <= 16'b0000_0000_0000_0000;
	end
end

always@(posedge currentchar[8])
begin
	if(currentchar[7:0] == phrase1[127:120])
		testcomarison = 1'b1;
	else testcomarison = 1'b0;
	
	if(acceptguesses == 2'b10)
		newwhoshome1 = whoshome10 | whoshome11 | whoshome12 
		| whoshome13 | whoshome14 | whoshome15 | whoshome16 
		| whoshome17 | whoshome18 | whoshome19 | whoshome1A
		| whoshome1B | whoshome1C | whoshome1D | whoshome1E
		| whoshome1F;
end

always@(posedge clock50)
begin
	
	if(acceptguesses == 2'b00)
	begin
		s <= before;
	end
	else if (acceptguesses <= 2'b01)
		s <= before;
	else
	begin
		s <= ns; // update guesses remaining
		whoshome1 <= newwhoshome1;
	end
end

always@(*)
begin
	case(s)
		before: gameoutcome = 2'b00;
		guess5:
		begin
			gameoutcome = 2'b00;
		end
		guess4:
		begin
			gameoutcome = 2'b00;
		end
		guess3:
		begin
			gameoutcome = 2'b00;
		end
		guess2:
		begin
			gameoutcome = 2'b00;
		end
		guess1:
		begin
			gameoutcome = 2'b00;
		end
		guess0:
		begin
			gameoutcome = 2'b01;
		end
	endcase
end

parameter pressedA = 16'HF01C;
parameter pressedB = 16'HF032;
parameter pressedC = 16'hF021;
parameter pressedD = 16'hF023;
parameter pressedE = 16'hF024;
parameter pressedF = 16'hF02B;
parameter pressedG = 16'hF034;
parameter pressedH = 16'hF033;
parameter pressedI = 16'hF043;
parameter pressedJ = 16'hF03B;
parameter pressedK = 16'hF042;
parameter pressedL = 16'hF04B;
parameter pressedM = 16'hF03A;
parameter pressedN = 16'hF031;
parameter pressedO = 16'hF044;
parameter pressedP = 16'hF04D;
parameter pressedQ = 16'hF015;
parameter pressedR = 16'hF02D;
parameter pressedS = 16'Hf01B;
parameter pressedT = 16'Hf02C;
parameter pressedU = 16'hF03C;
parameter pressedV = 16'hF02A;
parameter pressedW = 16'hF01D;
parameter pressedX = 16'hF022;
parameter pressedY = 16'hF035;
parameter pressedZ = 16'hF01A;
parameter pressedENTER = 16'hF05A;
parameter pressedSPACE = 16'hF029;
parameter pressedBACK = 16'hF066;
parameter pressedLEFT = 16'hF06B;
parameter pressedRIGHT = 16'hF074;
parameter pressedUP = 16'hF075;
parameter pressedDOWN = 16'hF072;

parameter uA = 8'h41; // upper case A-Z
parameter uB = 8'h42;
parameter uC = 8'h43;
parameter uD = 8'h44;
parameter uE = 8'h45;
parameter uF = 8'h46;
parameter uG = 8'h47;
parameter uH = 8'h48;
parameter uI = 8'h49;
parameter uJ = 8'h4A;
parameter uK = 8'h4B;
parameter uL = 8'h4C;
parameter uM = 8'h4D;
parameter uN = 8'h4E;
parameter uO = 8'h4F;
parameter uP = 8'h50;
parameter uQ = 8'h51;
parameter uR = 8'h52;
parameter uS = 8'h53;
parameter uT = 8'h54;
parameter uU = 8'h55;
parameter uV = 8'h56;
parameter uW = 8'h57;
parameter uX = 8'h58;
parameter uY = 8'h59;
parameter uZ = 8'h5A;

endmodule
